package com.captain.special.字符串;

/**
 * Des:
 *
 * @author XL
 * @Date 2022/4/6 9:13
 */
public class 匹配子串开始的索引位置 {


    public int findSubstringIndex(String text, String pattern) {
        char[] textChars = text.toCharArray();
        char[] patterChars = pattern.toCharArray();
        int tLen = textChars.length;
        int pLen = patterChars.length;

        int pi = 0, ti = 0;
        while (pi < pLen && ti < tLen) {
            if (textChars[ti] == patterChars[pi]) {
                ti++;
                pi++;
            } else {
                ti = ti - pi + 1;
                pi = 0;
            }
        }
        return (pi == pLen) ? (ti - pi) : -1;
    }


}
